package com.styytech.dadabao.utils;

import java.io.File;
import java.io.FileInputStream;
import java.security.KeyStore;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager;

import android.content.Context;


public class NonTrustManager implements X509TrustManager {
	
	X509TrustManager myJSSEX509TrustManager;
	
	public NonTrustManager(Context context) throws Exception {
		
     try {
		KeyStore ks = KeyStore.getInstance("BKS");
		
//		final String keyStorPath=CommonString.keyStoreDirectory()+CommonString.myKeystoreName;
		 final String keyStorPath = "";
		 //证书地址

		if(!new File(keyStorPath).exists()){;
		return ;
		}
        FileInputStream in = new FileInputStream(keyStorPath); 
		
		try{
			ks.load(in, "mysecret".toCharArray());
		}finally{
			in.close();
		}

		TrustManagerFactory tmf = TrustManagerFactory.getInstance("X509");
		tmf.init(ks);
		TrustManager tms[] = tmf.getTrustManagers();
		for (int i = 0; i < tms.length; i++) {
			if (tms[i] instanceof X509TrustManager) {
				myJSSEX509TrustManager = (X509TrustManager) tms[i];
				return;
			}
		}
     }
		catch (Exception e) {
			throw e;
		}
	}
	
	@Override
	public void checkClientTrusted(X509Certificate[] chain, String authType)
			throws CertificateException {
		myJSSEX509TrustManager.checkClientTrusted(chain, authType);

	}
	@Override
	public void checkServerTrusted(X509Certificate[] chain, String authType)
			throws CertificateException {
		myJSSEX509TrustManager.checkServerTrusted(chain, authType);
	}
	@Override
	public X509Certificate[] getAcceptedIssuers() {
		return null;
	}

}